package com.example.vue.config;

import com.example.vue.jwt.interceptor.AuthenticationInterceptor;
import com.example.vue.sys.interceptor.AccessInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * web方面的配置处理
 * 配置跨域
 * 拦截器
 * 资源无法访问
 *
 * @author longdeng
 * @Date 2019/10/7 19:30
 */
@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 权限拦截器
     *
     * @return
     */
    @Bean
    public AuthenticationInterceptor authenticationInterceptor() {
        return new AuthenticationInterceptor();
    }

    /**
     * 访问请求拦截器
     *
     * @return
     */
    @Bean
    public AccessInterceptor accessInterceptor() {
        return new AccessInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册访问拦截器
        registry.addInterceptor(accessInterceptor())
                .addPathPatterns("/**")
                .order(1)
                .excludePathPatterns("/error");
        // 注册权限拦截器
        registry.addInterceptor(authenticationInterceptor())
                .addPathPatterns("/**")
                .order(2)
                .excludePathPatterns("/error");
    }

    /**
     * 处理swagger无法访问资源问题
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 处理跨域问题
     *
     * @return
     */
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowCredentials(false)
                        .allowedMethods("GET", "POST");
            }
        };
    }


}
